#include "s3e.h"
#include "Iw2D.h"
#include "block.h"
#include "swipe.h"

SwipeObject::SwipeObject( int a_TouchID /*= 0*/ )
	: m_TouchID(a_TouchID)
	, m_Position(CIwSVec2(0,0))
	, m_LastPosition(CIwSVec2(0,0))
{
}
SwipeObject::~SwipeObject()
{
}

CIwSVec2 SwipeObject::GetSwipeMovement()
{
	static const int m_MinMoveDistance = int(BlockSize * BlockSize * SwipeMoveMult);

	if(m_Swiping && (m_Position - m_LastPosition).GetLengthSquaredUnshifted() > m_MinMoveDistance)
	{
		const CIwSVec2 delta = m_Position - m_LastPosition;
		m_LastPosition = m_Position;
		return delta;
	}
	return CIwSVec2(0, 0);
}

bool SwipeObject::GetSwiping()
{
	return m_Swiping;
}

void SwipeObject::Update(float deltaMs)
{
	// If we tapped last frame, reset it here.

	m_Tapped = false;
	const bool swiping = s3ePointerGetTouchState(m_TouchID) & S3E_POINTER_STATE_DOWN;
	const CIwSVec2 curPosition = CIwSVec2(s3ePointerGetTouchX(m_TouchID),s3ePointerGetTouchY(m_TouchID));
	if (swiping)
	{
		if(!m_Swiping)
		{
			m_StartPosition = curPosition;
			m_LastPosition = curPosition;
		}
		m_Position = curPosition;
	}
	else
	{
		if(m_Swiping)
		{
			if((curPosition - m_StartPosition).GetLengthSquaredUnshifted() < m_TapMarginSq)
				// If we released this frame, and it hasn't moved enough, we've tapped.
				m_Tapped = true;

			m_LastPosition = CIwSVec2(0, 0);
			m_Position = CIwSVec2(0, 0);
		}
	}
	m_Swiping = swiping;
}

//#include "s3e.h"
//#include "swipe.h"
//
//SwipeObject::SwipeObject( int a_TouchID /*= 0*/ )	:
//	m_TouchID(a_TouchID),
//	m_IsSwiping(false),
//	m_MaxPoints(20),
//	m_PointIntervalTimer(0.0f),
//	m_PointTTLTimer(0.0f),
//	m_PointTimeInterval(10.0f),
//	m_PointTTL(75.0f)
//{
//	m_Points = std::list<CIwSVec2>();
//}
//
//SwipeObject::~SwipeObject()
//{
//
//}
//
//void SwipeObject::Update( float deltaMs )
//{
//	m_PointIntervalTimer += deltaMs;
//	m_PointTTLTimer += deltaMs;
//
//	// If the screen is being touched...
//	if (s3ePointerGetTouchState(m_TouchID) & S3E_POINTER_STATE_DOWN)
//	{
//		m_IsSwiping = true;
//
//		// If sufficient time has passed, a new point has to be added
//		if (m_PointIntervalTimer >= m_PointTimeInterval)
//		{
//			m_PointIntervalTimer = 0.0f;
//
//			m_Points.push_back( CIwSVec2(s3ePointerGetTouchX(m_TouchID),s3ePointerGetTouchY(m_TouchID)) );
//
//			// If the list of points contains more points than the maximum allowed number...
//			if (m_Points.size() > m_MaxPoints)
//			{
//				// Remove the front point.
//				m_Points.pop_front();
//			}
//			if(m_Points.size() == 1)
//				m_PointTTLTimer = 0.0f;
//		}
//	}
//	// Else if the screen is not being touched...
//	else
//	{
//		// If the screen was being touched last frame...
//		if (m_IsSwiping)
//		{
//			// Clear the points list.
//			m_Points.clear();
//		}
//
//		m_IsSwiping = false;
//	}
//
//	// If the lifetime of the last point has passed...
//	if (m_PointTTLTimer >= m_PointTTL && m_Points.size() > 0)
//	{
//		// Remove that point.
//		//m_Points.pop_front();
//
//		//m_PointTTLTimer = 0.0f;
//	}
//}
//
//void SwipeObject::Render()
//{
//	CIwSVec2 currVec;
//	CIwSVec2 lastVec;
//	std::list<CIwSVec2>::iterator it = m_Points.begin();
//
//	if (m_Points.size() > 1)
//	{
//		for (currVec = (*it); it != m_Points.end(); ++it)
//		{
//			lastVec = currVec;
//			currVec = (*it);
//
//			// Draw a line from lastVec to currVec.
//			Iw2DSetColour(0xffffffff);
//			Iw2DDrawLine(lastVec, currVec);
//		}
//	}
//
//// 	char string[64];
//// 	sprintf(string, "Points: %i", m_Points.size());
//// 	s3eDebugOutputString(string);
//}
//
//CIwSVec2 SwipeObject::GetAverageVector()
//{
//	if (m_Points.size() >= 2)
//	{
//		std::list<CIwSVec2>::iterator it1 = m_Points.begin();
//		std::list<CIwSVec2>::iterator it2 = it1;
//		++it2;
//
//		CIwSVec2 vec(0, 0);
//
//		for (; it2 != m_Points.end(); ++it2, ++it1)
//		{
//			vec.x += (*it2).x - (*it1).x;
//			vec.y += (*it2).y - (*it1).y;
//		}
//
//		return vec;
//	}
//	else
//	{
//		return CIwSVec2(0, 0);
//	}
//}
//
//void SwipeObject::Reset()
//{
//	m_Points.clear();
//	m_PointTTLTimer = 0.0f;
//	m_PointIntervalTimer = 0.0f;
//	m_IsSwiping = false;
//}